import sys
import os
import csv
import math

# In order to import functions in the DrawingAPIs folder.
sys.path.append(os.getcwd() + '/3dparty/FigureDrawer/')

from DrawingAPIs.line_drawer import *

qos_fieldnames = ['Frame ID', 'Recall', 'Precision', 'F1 Score']
diff_fieldnames = ['Frame ID', 'Diff']

def LoadQoSLog(qos_log_file):
    x_tick_set, x_tick_pos, x_ticklabels = [], [], []
    recall_vals, precision_vals, f1_vals = [], [], []
    qos_val_set, legends = [], ['Recall', 'Precision', 'F1 Score']
    y_min, y_max = 0.0, 0.0
    x_min, x_max = 0, 0

    with open(qos_log_file, 'r') as qos_f:
        qos_reader = csv.DictReader(qos_f)
        temp_x = []
        for row in qos_reader:
            temp_x.append(int(row['Frame ID']))
            recall_vals.append(float(row['Recall']))
            precision_vals.append(float(row['Precision']))
            f1_vals.append(float(row['F1 Score']))

        x_max = max(temp_x) + 1
        x_tick_set = [temp_x, temp_x, temp_x]
        y_max = max([max(recall_vals), max(precision_vals), max(f1_vals)])
        qos_val_set = [recall_vals, precision_vals, f1_vals]

    if y_max > math.ceil(y_max) - 0.5:
        y_max = math.ceil(y_max)
    else:
        y_max = math.ceil(y_max) - 0.5

    x_tick_pos = list(range(1, x_max, 2))
    x_ticklabels = [str(x) for x in x_tick_pos]

    return x_min, x_max, y_min, y_max, x_tick_set, x_tick_pos, x_ticklabels, \
        qos_val_set, legends

def LoadDiffLog(diff_log_file):
    x_tick_set, x_tick_pos, x_ticklabels = [], [], []
    x_min, x_max, y_min, y_max = 0, 0, 0.0, 0.0
    diff_vals = []

    with open(diff_log_file, 'r') as diff_f:
        temp_x = []
        temp_diff = []
        diff_reader = csv.DictReader(diff_f)
        for row in diff_reader:
            temp_x.append(int(row['Frame ID']))
            temp_diff.append(float(row['Diff']))

        x_max = max(temp_x) + 1
        x_tick_set.append(temp_x)
        y_max = max(temp_diff)
        diff_vals.append(temp_diff)
    
    if y_max > math.ceil(y_max) - 0.5:
        y_max = math.ceil(y_max)
    else:
        y_max = math.ceil(y_max) - 0.5

    x_tick_pos = list(range(1, x_max, 2))
    x_ticklabels = [str(x) for x in x_tick_pos]
    
    return x_min, x_max, y_min, y_max, x_tick_set, x_tick_pos, x_ticklabels, \
        diff_vals

def DrawQoSFig(qos_log_file):
    x_min, x_max, y_min, y_max, x_tick_set, x_tick_pos, x_ticklabels, qos_val_set, legends = LoadQoSLog(qos_log_file)

    print('Drawing mot_qos.pdf')
    DrawLineFig(x_tick_set, x_tick_pos, x_ticklabels, qos_val_set, \
        legends, x_label = 'Frame ID', y_label = '', x_min = x_min, x_max = x_max, \
        y_min = y_min, y_max = y_max, folder = './', filename = 'mot_qos.pdf', \
        allow_legend = True, out_format='pdf')

def DrawDiffFig(diff_log_file):
    x_min, x_max, y_min, y_max, x_tick_set, x_tick_pos, x_ticklabels, diff_vals = \
        LoadDiffLog(diff_log_file)

    print('Drawing mot_diff.pdf')
    DrawLineFig(x_tick_set, x_tick_pos, x_ticklabels, diff_vals, "", \
                x_label = 'Frame ID', y_label = '$\delta_{diff}$', x_min = x_min, \
                x_max = x_max, y_min = y_min, y_max = y_max, folder = './', \
                filename = 'mot_diff.pdf', allow_legend = False, out_format='pdf')

if __name__ == '__main__':
    DrawQoSFig('./qos_log.csv')
    DrawDiffFig('./diff_log.csv')